package com.java110.mall.cart.api;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.java110.mall.cart.bmo.storeOrderUp.IDeleteStoreOrderUpBMO;
import com.java110.mall.cart.bmo.storeOrderUp.IGetStoreOrderUpBMO;
import com.java110.mall.cart.bmo.storeOrderUp.ISaveStoreOrderUpBMO;
import com.java110.mall.cart.bmo.storeOrderUp.IUpdateStoreOrderUpBMO;
import com.java110.dto.storeOrder.StoreOrderUpDto;
import com.java110.po.storeOrder.StoreOrderUpPo;
import com.java110.utils.util.Assert;
import com.java110.utils.util.BeanConvertUtil;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "/storeOrderUp")
public class StoreOrderUpApi {

    @Autowired
    private ISaveStoreOrderUpBMO saveStoreOrderUpBMOImpl;
    @Autowired
    private IUpdateStoreOrderUpBMO updateStoreOrderUpBMOImpl;
    @Autowired
    private IDeleteStoreOrderUpBMO deleteStoreOrderUpBMOImpl;

    @Autowired
    private IGetStoreOrderUpBMO getStoreOrderUpBMOImpl;

    /**
     * 微信保存消息模板
     * @serviceCode /storeOrderUp/saveStoreOrderUp
     * @path /app/storeOrderUp/saveStoreOrderUp
     * @param reqJson
     * @return
     */
    @RequestMapping(value = "/saveStoreOrderUp", method = RequestMethod.POST)
    public ResponseEntity<String> saveStoreOrderUp(@RequestBody JSONObject reqJson,
                                                   @RequestHeader(value="user-id") String userId) {

        System.out.println( reqJson.toJSONString() );
        System.out.println( userId );
        JSONObject storeOrderUp = reqJson.getJSONObject( "storeOrderUp" );
        if(null == storeOrderUp || storeOrderUp.size() <= 0){
            return ResultVo.createResponseEntity(ResultVo.CODE_OK, "没有传递订单信息");
        }
        Assert.hasKeyAndValue(storeOrderUp, "totalPrice", "请求报文中未包含totalPrice");
        Assert.hasKeyAndValue(storeOrderUp, "shopId", "请求报文中未包含shopId");
        Assert.hasKeyAndValue(storeOrderUp, "state", "请求报文中未包含state");
        storeOrderUp.put( "userId",userId );
        JSONArray productStocks = reqJson.getJSONArray( "productStocks" );
        if(null == productStocks || productStocks.size() <= 0){
            return ResultVo.createResponseEntity(ResultVo.CODE_OK, "没有传递订单信息");
        }

        return saveStoreOrderUpBMOImpl.save(storeOrderUp,productStocks);
    }

    /**
     * 微信修改消息模板
     * @serviceCode /storeOrderUp/updateStoreOrderUp
     * @path /app/storeOrderUp/updateStoreOrderUp
     * @param reqJson
     * @return
     */
    @RequestMapping(value = "/updateStoreOrderUp", method = RequestMethod.POST)
    public ResponseEntity<String> updateStoreOrderUp(@RequestBody JSONObject reqJson) {

        Assert.hasKeyAndValue(reqJson, "shopId", "请求报文中未包含shopId");
        Assert.hasKeyAndValue(reqJson, "state", "请求报文中未包含state");
        Assert.hasKeyAndValue(reqJson, "orderUpId", "orderUpId不能为空");


        StoreOrderUpPo storeOrderUpPo = BeanConvertUtil.covertBean(reqJson, StoreOrderUpPo.class);
        return updateStoreOrderUpBMOImpl.update(storeOrderUpPo);
    }

    /**
     * 微信删除消息模板
     * @serviceCode /storeOrderUp/deleteStoreOrderUp
     * @path /app/storeOrderUp/deleteStoreOrderUp
     * @param reqJson
     * @return
     */
    @RequestMapping(value = "/deleteStoreOrderUp", method = RequestMethod.POST)
    public ResponseEntity<String> deleteStoreOrderUp(@RequestBody JSONObject reqJson) {
        Assert.hasKeyAndValue(reqJson, "shopId", "商铺ID不能为空");
        Assert.hasKeyAndValue(reqJson, "orderUpId", "orderUpId不能为空");


        StoreOrderUpPo storeOrderUpPo = BeanConvertUtil.covertBean(reqJson, StoreOrderUpPo.class);
        return deleteStoreOrderUpBMOImpl.delete(storeOrderUpPo);
    }

    /**
     * 微信删除消息模板
     * @serviceCode /storeOrderUp/queryStoreOrderUp
     * @path /app/storeOrderUp/queryStoreOrderUp
     * @param shopId 小区ID
     * @return
     */
    @RequestMapping(value = "/queryStoreOrderUp", method = RequestMethod.GET)
    public ResponseEntity<String> queryStoreOrderUp(@RequestParam(value = "shopId" , required = false) String shopId,
                                                      @RequestParam(value = "vipId" , required = false) String vipId,
                                                      @RequestParam(value = "personId" , required = false) String personId,
                                                      @RequestParam(value = "orderUpId" , required = false) String orderUpId,
                                                      @RequestParam(value = "page") int page,
                                                      @RequestParam(value = "row") int row) {
        StoreOrderUpDto storeOrderUpDto = new StoreOrderUpDto();
        storeOrderUpDto.setPage(page);
        storeOrderUpDto.setRow(row);
        storeOrderUpDto.setShopId(shopId);
        storeOrderUpDto.setVipId(vipId);
        storeOrderUpDto.setPersonId(personId);
        storeOrderUpDto.setOrderUpId(orderUpId);
        return getStoreOrderUpBMOImpl.get(storeOrderUpDto);
    }
}
